var stompClient=null;

function connect() {
    //创建socket连接
    var socket= new SockJS("/chat");
    //服务器和浏览器握手
    stompClient = Stomp.over(socket);

    stompClient.connect({},function (frame) {
        stompClient.subscribe('/topic/greetings',function (greeting) {
            showGreeting(JSON.parse(greeting.body));
        })
    })
}

function disconnect() {
    if(stompClient!==null){
        stompClient.disconnect();
    }
}
function sendName() {
    stompClient.send("/app/hello",{},JSON.stringify({'content':$("#content").val()}))
    $("#content").val("");
}
function showGreeting(message) {
    $("#greetings").append("<div>"+message.name+":"+message.content+"</div>");
}
$(document).ready(function(){
    connect();
    window.onbeforeunload=function(){
        disconnect();
    }
    $("#send").click(function () {
        sendName();
    })
})
